using UFrame.BridgeUI;
using UFrame.BridgeUI.Graph;

using UnityEngine;
using UnityEngine.Events;

public partial class UIAgent : AgentContext<UIAgent, IUIFacade>
{
    protected Canvas m_canvas;
    protected Canvas[] m_otherCanvas;
    public static Canvas MainCanvas => Context.m_canvas;

    protected override IUIFacade CreateAgent()
    {
        if(m_agent == null)
        {
            return new SingleUIFacade();
        }
        return base.CreateAgent();
    }

    public static void StartWithGraph(Canvas canvas, string graphPath)
    {
        var facade = new SingleRuntimeUIFacade();
        facade.BindUIParent(canvas.transform);
        var uiData = Resources.Load<UIGraph>(graphPath);
        facade.RegistUIData(uiData);
        Context.m_agent = facade;
        Context.m_canvas = canvas;
    }

    public static void StartWithGraph(Canvas canvas, UIGraph graph,params UIGraph[] otherGraphs)
    {
        var facade = new SingleRuntimeUIFacade();
        facade.RegistUIData(graph);
        if(otherGraphs != null && otherGraphs.Length >0)
        {
            foreach (var subGraph in otherGraphs)
            {
                facade.RegistUIData(subGraph);
            }
        }
        Context.m_agent = facade;
        Context.m_canvas = canvas;
    }

    public static void StartWithCanvas(Canvas canvas, params Canvas[] otherCanvas)
    {
        Context.m_agent = new MutiUIFacade();
        Context.m_canvas = canvas;
        Context.m_otherCanvas = otherCanvas;

        if (canvas)
        {
            var group = canvas.GetComponentInChildren<PanelGroup>();
            if (group)
            {
                Instance.RegistGroup(group);
            }
            if (otherCanvas != null)
            {
                for (int i = 0; i < otherCanvas.Length; i++)
                {
                    var canvasItem = otherCanvas[i];
                    var otherGroup = canvasItem?.GetComponentInChildren<PanelGroup>();
                    if (otherGroup)
                    {
                        Instance.RegistGroup(otherGroup);
                    }
                }
            }
        }
    }

    public static void RegistGroup(PanelGroup group)
    {
        if (Instance != null)
            Instance.RegistGroup(group);
    }

    public static void Close(string panelName)
    {
        if (Instance != null)
            Instance.Close(panelName);
    }

    public static void Close(IPanelGroup parentGroup, string panelName)
    {
        if (Instance != null)
            Instance.Close(parentGroup, panelName);
    }

    public static bool Exist(string panelName)
    {
        if (Instance != null && Instance.Exist(panelName))
            return true;
        return false;
    }

    public static void Hide(string panelName)
    {
        if (Instance != null)
            Instance.Hide(panelName);
    }

    public static void Hide(IPanelGroup parentGroup, string panelName)
    {
        if (Instance != null)
            Instance.Hide(parentGroup, panelName);
    }

    public static bool IsPanelOpen(string panelName)
    {
        if (Instance != null && Instance.IsPanelOpen(panelName))
            return true;
        return false;
    }

    public static bool IsPanelOpen<T>(string panelName, out T[] panels)
    {
        if (Instance != null && Instance.IsPanelOpen(panelName, out panels))
            return true;
        panels = null;
        return false;
    }

    public static void Open(string panelName, object data = null)
    {
        if (Instance != null)
            Instance.Open(panelName, data);
    }

    public static void Open(IUIPanel parentPanel, string panelName, object data = null)
    {
        if (Instance != null)
            Instance.Open(parentPanel, panelName, data);
    }

    public static void Open(string panelName, IPanelVisitor uiData)
    {
        if (Instance != null)
            Instance.Open(panelName, uiData);
    }

    public static void Open(IUIPanel parentPanel, string panelName, IPanelVisitor uiData)
    {
        if (Instance != null)
            Instance.Open(parentPanel, panelName, uiData);
    }

    public static void RegistClose(UnityAction<IUIPanel> onClose)
    {
        if (Instance != null)
            Instance.RegistClose(onClose);
    }

    public static void RegistCreate(UnityAction<IUIPanel> onCreate)
    {
        if (Instance != null)
            Instance.RegistCreate(onCreate);
    }

    public static void RegistGroup(IPanelGroup group)
    {
        if (Instance != null)
            Instance.RegistGroup(group);
    }

    public static void RemoveClose(UnityAction<IUIPanel> onClose)
    {
        if (Instance != null)
            Instance.RemoveClose(onClose);
    }

    public static void RemoveCreate(UnityAction<IUIPanel> onCreate)
    {
        if (Instance != null)
            Instance.RemoveCreate(onCreate);
    }

    public static void RemoveGroup(IPanelGroup group)
    {
        if (Instance != null)
            Instance.RemoveGroup(group);
    }
}